import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import Users from '@/components/user/Users.vue'
import SelectUser from '@/components/user/pages/SelectUser.vue'
import Test from '@/components/user/Test.vue'
import System from '@/components/user/SystemMenu.vue'
import Roules from '@/components/roules/Roules.vue'
import UserRoule from '@/components/roules/userRoule.vue'
import Department from '@/components/user/Department.vue'
import Dict from '@/components/dict/index.vue'
import Data from '@/components/dict/data.vue'
import Menu from '@/components/menu/index.vue'
import DataTableCad from '@/components/menu/dataTableCard'
import Online from '@/components/online/index.vue'
import Tasks from '@/components/tasks/index.vue'
import Service from '@/components/service/index.vue'
import JournalView from '@/components/tasks/views/journalView.vue'
import OperationLog from '@/components/operationLog/index.vue'
import PersonInfo from '@/components/personInfo/index.vue'
import TemplateManagement from '@/components/shortMessage/templateManagement/index.vue'
import SignatureManagement from '@/components/shortMessage/signatureManagement/index.vue'
import QuerySms from '@/components/shortMessage/querySms/index.vue'
import EmailManagement from '@/components/emailManagement/index.vue'
import FileManagement from '@/components/fileManagement/index.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      component: Login
    }, {
      path: '/',
      redirect: '/login',
    }, {
      path: '/home',
      component: Home,
      redirect: '/users',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/department', component: Department },
        { path: '/dict', component: Dict },
        { path: '/menu', component: Menu },
        { path: '/test', component: Test },
        {
          path: '/system',
          component: System
        },
        {
          path: '/roles',
          component: Roules
        },
        {
          path: '/online',
          component: Online
        },
        {
          path: '/tasks',
          component: Tasks
        },
        {
          path: '/service',
          component: Service
        },
        {
          path: '/operationLog',
          component: OperationLog
        },
        {
          path: '/journalView',
          component: JournalView
        },
        {
          path: '/data',
          component: Data
        }, {
          path: '/dataTableCad',
          component: DataTableCad
        },
        {
          path: '/userRoule',
          component: UserRoule
        },
        {
          path: '/selectUser',
          component: SelectUser
        },
        {
          path: '/personInfo',
          component: PersonInfo
        },
        {
          path: '/templateManagement',
          component: TemplateManagement
        },
        {
          path: '/signatureManagement',
          component: SignatureManagement 
        },
        {
          path: '/querySms',
          component: QuerySms
        },
        {
          path: '/emailManagement',
          component: EmailManagement
        },
        {
          path: '/fileManagement',
          component: FileManagement
        }
      ]
    },
  ]
})

// 挂在路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next() 放行 next('login') 强制跳转
  if (to.path === '/login') return next();
  // 获取token
  const tokenStr = sessionStorage.getItem('satoken');
  // console.log(tokenStr);
  if (!tokenStr) return next('/login');
  next();
})

export default router
